//
//  JYJSortContentCollectionViewCell.m
//  HappyListen
//
//  Created by 冀永金 on 16/9/3.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJSortContentCollectionViewCell.h"
#import "BaseTableView.h"
#import "JYJTAMacro.h"
#import "Masonry.h"
#import "JYJColumnListTableViewCell.h"
#import "JYJProgramListTableViewCell.h"
#import "BaseLabel.h"
#import "JYJNetWorkParser.h"
#import "JYJSortModel.h"
#import "JYJColumnListModel.h"
#import "JYJProgramListModel.h"
#import "JYJProgramListSecondModel.h"
#import "JYJVCOfFeaturedTopThreeDetails.h"
#import "JYJChannelModel.h"

@interface JYJSortContentCollectionViewCell ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray <JYJColumnListModel *> *marrOfTopLists;
@property (nonatomic, strong) NSMutableArray <JYJProgramListModel *> *marrOfProgramList;
@property (nonatomic, strong) NSMutableArray <JYJProgramListSecondModel *>*marrOfModel;
@property (nonatomic, strong) UIImageView *imageOfAnimation;
@end
@implementation JYJSortContentCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
        [self createAnimation];
    }
    return self;
}
- (void)setSortModel:(JYJSortModel *)sortModel {
    _sortModel = sortModel;
    NSString *str1 = @"json=%7B%22channel_id%22%3A%22";
    NSString *string = [NSString stringWithFormat:@"%ld",self.sortModel.channel_id];
    NSString *str3 = BRACKETS_STR;
    string = [[str1 stringByAppendingString:string] stringByAppendingString:str3];
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/channelinfo.jhtml" andBody:string andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
        self.marrOfTopLists = [NSMutableArray array];
        self.marrOfProgramList = [NSMutableArray array];
        self.marrOfModel = [NSMutableArray array];
        NSArray *topListArray = result[@"data"][@"top_items"];
        for (NSDictionary *dic in topListArray) {
            JYJColumnListModel *model = [JYJColumnListModel modelWithDic:dic];
            [self.marrOfTopLists addObject:model];
        }
        NSArray  *programArray = result[@"data"][@"top_musics"];
        NSArray  *programArrayTwo = result[@"data"][@"new_musics"];
       
            for (NSDictionary *dic in programArray) {
                JYJProgramListModel *model = [JYJProgramListModel modelWithDic:dic];
                [self.marrOfProgramList addObject:model];
            }
            for (NSDictionary *dic in programArrayTwo) {
                JYJProgramListSecondModel *model = [JYJProgramListSecondModel modelWithDic:dic];
                [self.marrOfModel addObject:model];
            }

        
        [self.tableView reloadData];
        self.imageOfAnimation.hidden = YES;
    } andFailureBlock:^(NSError *error) {
        
    }];
    
    

}
-(void)setChannelModel:(JYJChannelModel *)channelModel {
    _channelModel = channelModel;
    NSString *str1 = @"json=%7B%22channel_id%22%3A%22";
    NSString *string = [NSString stringWithFormat:@"%ld",self.channelModel.channel_id];
    NSString *str3 = BRACKETS_STR;
    string = [[str1 stringByAppendingString:string] stringByAppendingString:str3];
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/channelinfo.jhtml" andBody:string andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
        self.marrOfTopLists = [NSMutableArray array];
        self.marrOfProgramList = [NSMutableArray array];
        self.marrOfModel = [NSMutableArray array];
        NSArray *topListArray = result[@"data"][@"top_items"];
        for (NSDictionary *dic in topListArray) {
            JYJColumnListModel *model = [JYJColumnListModel modelWithDic:dic];
            [self.marrOfTopLists addObject:model];
        }
        NSArray  *programArray = result[@"data"][@"top_musics"];
        NSArray  *programArrayTwo = result[@"data"][@"new_musics"];
        
        for (NSDictionary *dic in programArray) {
            JYJProgramListModel *model = [JYJProgramListModel modelWithDic:dic];
            [self.marrOfProgramList addObject:model];
        }
        for (NSDictionary *dic in programArrayTwo) {
            JYJProgramListSecondModel *model = [JYJProgramListSecondModel modelWithDic:dic];
            [self.marrOfModel addObject:model];
        }
        
        
        [self.tableView reloadData];
        self.imageOfAnimation.hidden = YES;
    } andFailureBlock:^(NSError *error) {
        
    }];
    

}
- (void)createTableView {
    self.tableView = [[BaseTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.contentView addSubview:_tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[JYJColumnListTableViewCell class] forCellReuseIdentifier:@"Column"];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = [UIColor colorWithRed:0.07 green:0.07 blue:0.08 alpha:1.00];
    [self.tableView registerClass:[JYJProgramListTableViewCell class] forCellReuseIdentifier:@"Program"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.tableView.frame = self.contentView.bounds;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.marrOfTopLists.count;
    }else if (section == 1){
        return self.marrOfProgramList.count;
    }else {
       return self.marrOfModel.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        JYJColumnListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Column" forIndexPath:indexPath];
        JYJColumnListModel *model = self.marrOfTopLists[indexPath.row];
        [cell setListModel:model];
        return cell;
        
    }else {
        JYJProgramListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Program" forIndexPath:indexPath];
        NSArray *countArray = @[@"1",@"2",@"3"];
        cell.countLabel.text = countArray[indexPath.row];
        if (indexPath.section == 1) {
            JYJProgramListModel *model = self.marrOfProgramList[indexPath.row];
            [cell setProgramListModel:model];
        }else {
            JYJProgramListSecondModel *model = self.marrOfModel[indexPath.row];
            [cell setListSecondModel:model];
        }
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *HeadView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
    HeadView.backgroundColor = [UIColor colorWithRed:0.07 green:0.07 blue:0.08 alpha:1.00];
    UIImageView *fireImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"catinfo_fire"]];
    [HeadView addSubview:fireImage];
    [fireImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(HeadView.mas_top).with.offset(5);
        make.left.equalTo(HeadView.mas_left).with.offset(20);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(25);
    }];
    UILabel *labelOfText = [[UILabel alloc]init];
    [HeadView addSubview:labelOfText];
    labelOfText.textColor = [UIColor redColor];
    labelOfText.textAlignment = 1;
    [labelOfText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(HeadView.mas_top).with.offset(5);
        make.left.equalTo(fireImage.mas_right).with.offset(5);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
    }];
    NSArray *arr = @[@"最热栏目榜",@"最热节目榜",@"最新节目榜"];
    labelOfText.text = arr[section];
    UILabel *topLabel = [[UILabel alloc]init];
    [HeadView addSubview:topLabel];
    topLabel.textAlignment = 1;
    topLabel.textColor = [UIColor lightGrayColor];
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(HeadView.mas_top).with.offset(3);
        make.left.equalTo(labelOfText.mas_right).with.offset(0);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(35);
    }];
    UIView *colorView = [[UIView alloc]init];
    [HeadView addSubview:colorView];
    colorView.backgroundColor = [UIColor orangeColor];
    [colorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(HeadView.mas_bottom).with.offset(-10);
        make.left.equalTo(HeadView.mas_left).with.offset(0);
        make.right.equalTo(HeadView.mas_right).with.offset(0);
        make.height.mas_equalTo(2);
    }];
    topLabel.text = @"TOP3";
    return HeadView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
/** 让Plain风格的tableView的标题不悬停. */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat sectionHeaderHeight = 40;
    CGFloat sectionFooterHeight = 10;
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY >= 0 && offsetY <= sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-offsetY, 0, -sectionFooterHeight, 0);
    }else if (offsetY >= sectionHeaderHeight && offsetY <= scrollView.contentSize.height - scrollView.frame.size.height - sectionFooterHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -sectionFooterHeight, 0);
    }else if (offsetY >= scrollView.contentSize.height - scrollView.frame.size.height - sectionFooterHeight && offsetY <= scrollView.contentSize.height - scrollView.frame.size.height)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-offsetY, 0, -(scrollView.contentSize.height - scrollView.frame.size.height - sectionFooterHeight), 0);
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        JYJVCOfFeaturedTopThreeDetails *details = [[JYJVCOfFeaturedTopThreeDetails alloc]init];
         JYJColumnListModel *model = self.marrOfTopLists[indexPath.row];
        details.columnListModel = model;
        self.handleAction(details);
    }else {
        JYJVCOfFeaturedTopThreeDetails *details = [[JYJVCOfFeaturedTopThreeDetails alloc]init];
        if (indexPath.section == 1) {
            JYJProgramListModel *model = self.marrOfProgramList[indexPath.row];
            details.programListModel = model;
        }else {
            JYJProgramListSecondModel *model = self.marrOfModel[indexPath.row];
            details.programListModelTwo = model;
           
        }

        self.handleAction(details);
    }
}
- (void)createAnimation {
    self.imageOfAnimation = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"main_load_0"]];
    self.imageOfAnimation.frame = CGRectMake(SCREEN_WIDTH / 2 - 30, 200, 80, 200);
    [self addSubview:_imageOfAnimation];
    NSMutableArray *marr = [NSMutableArray array];
    for (int i = 0; i < 5; i++) {
        NSString *imageName = [NSString stringWithFormat:@"main_load_%d",i];
        UIImage *image = [UIImage imageNamed:imageName];
        [marr addObject:image];
    }
    _imageOfAnimation.animationDuration = 1;
    _imageOfAnimation.animationImages  = marr;
    _imageOfAnimation.animationRepeatCount = 0;
    [ _imageOfAnimation startAnimating];
    
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
